package com.taplinker.client.acount.rpc;

import android.content.Context;
import android.util.Log;

import com.taplinker.client.AppConfigContext;
import com.taplinker.client.AppConfigFactory;
import com.taplinker.client.domain.UserInfo;
import com.taplinker.client.im.domain.ImDataCenter;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.rpc.command.SimpleBaseCommand;
import com.taplinker.core.rpc.http.HttpTemplate;
import com.taplinker.dto.web.UpdateUserInfoRequest.UpdateUserInfoReq;
import com.taplinker.dto.web.UpdateUserInfoRequest.UpdateUserInfoReq.Builder;
/**
 * 更新用户信息
 * @author djbian
 *
 */
public class UpdateUserInfoCommad extends SimpleBaseCommand {
	private static final String TAG = "com.taplinker.client.acount.rpc.UpdateUserInfoCommad";
	
	private String email = AppConfigFactory.getInstance().getUserInfo().getEmail();
	private String icon = AppConfigFactory.getInstance().getUserInfo().getUserIcon();
	private String nickname = AppConfigFactory.getInstance().getUserInfo().getNickname();
	private String phone = AppConfigFactory.getInstance().getUserInfo().getPhone();
	
	public UpdateUserInfoCommad() {
	}
	
	public UpdateUserInfoCommad(String email, String icon, String nickname, String phone, Context context) {
		super( context);
		this.email = email;
		this.icon = icon;
		this.nickname = nickname;
		this.phone = phone;
	}
	
	public UpdateUserInfoCommad(UserInfo userInfo, Context context) {
		super(context);
		this.email = userInfo.getEmail();
		this.icon = userInfo.getUserIcon();
		this.nickname = userInfo.getNickname();
		this.phone = userInfo.getPhone();
	}
	
	public UpdateUserInfoCommad(String icon, Context context) {
		super(context);
		this.icon = icon;
	}
	
	@Override
	public Result go() {
		Builder res = UpdateUserInfoReq.newBuilder();
		res.setEmail(email);
		res.setIcon(icon);
		res.setNickname(nickname);
		res.setPhone(phone);
		return new HttpTemplate().postForGogoByte(AppConfigContext.TAPLINKER_WEB
				+ "/services/userprofile/v1/user/info/"
				+ AppConfigFactory.getInstance().getUserInfo().getUserid(),
				res.build().toByteArray());
	}

	@Override
	public void yes(Result result) {
		try {
			AppConfigFactory.getInstance().getUserInfo().setUserIcon(icon);
			AppConfigFactory.getInstance().getUserInfo().setEmail(email);
			AppConfigFactory.getInstance().getUserInfo().setNickname(nickname);
			AppConfigFactory.getInstance().getUserInfo().setPhone(phone);
			ImDataCenter.getInstance().noticeListener(ImDataCenter.UPDATE_USER_INFO);
		} catch (Exception e){
			Log.d(TAG, "" + e.getMessage());
		}
	}
}
